﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 在实现数组后的简易超市
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //int N = 5;
            //Console.WriteLine("----------------------------");
            //Console.WriteLine("  欢迎使用简易超市管理系统");
            //Console.WriteLine("----------------------------");
            //bool started = true;
            //string[] names = new string[N];
            //double[] prices = new double[N];
            //int count = 0;
            //do
            //{
            //    Console.WriteLine("\n请选择操作: ");
            //    Console.WriteLine("1.录入商品");
            //    Console.WriteLine("2.查看商品");
            //    Console.WriteLine("3.下架商品");
            //    Console.WriteLine("4.退出系统");
            //    int num = Convert.ToInt32(Console.ReadLine());
            //    switch (num)
            //    {
            //        case 1:
            //            while (true)
            //            {
            //                Console.WriteLine("\n请输入商品名称: ");
            //                string name = Console.ReadLine();
            //                names[count] = name;
            //                Console.WriteLine("请输入商品价格: ");
            //                double price = Convert.ToDouble(Console.ReadLine());
            //                prices[count] = price;
            //                count++;
            //                Console.WriteLine("是否选择重新录入? y | n");
            //                string flag = Console.ReadLine();
            //                if (!(flag == "y"))
            //                {
            //                    break;
            //                }
            //            }
            //            break;
            //        case 2:
            //            Console.WriteLine("\n商品名称\t商品价格");
            //            for (int i = 0; i < count; i++)
            //            {
            //                Console.WriteLine($"{names[i]}\t{prices[i]}");
            //            }
            //            break;
            //        case 3:
            //            Console.WriteLine("请选择要下架的商品序号: ");
            //            int index = Convert.ToInt32(Console.ReadLine());
            //            for (int i = index + 1; i < names.Length; i++)
            //            {
            //                names[i - 1] = names[i];
            //                prices[i - 1] = prices[i];
            //            }
            //            names[count - 1] = null;
            //            prices[count - 1] = 0.0;
            //            count--;
            //            Console.WriteLine("下架商品成功");

            //            break;
            //        case 4:
            //            Console.WriteLine("感谢使用，再见!");
            //            started = false;
            //            break;
            //        default:
            //            Console.ForegroundColor = ConsoleColor.Red;
            //            Console.WriteLine("\n输入的操作编号不存在！");
            //            Console.ResetColor();
            //            break;
            //    }

            //}
            //while (started);

            int N = 5;
            Console.WriteLine("----------------------------");
            Console.WriteLine("  欢迎使用简易超市管理系统");
            Console.WriteLine("----------------------------");
            bool started = true;
            string[] names = new string[N];
            double[] prices = new double[N];
            int count = 0;
            do
            {
                Console.WriteLine("\n请选择操作: ");
                Console.WriteLine("1.录入商品");
                Console.WriteLine("2.查看商品");
                Console.WriteLine("3.查看最新商品");
                Console.WriteLine("4.下架商品");
                Console.WriteLine("5.退出系统");
                int num = Convert.ToInt32(Console.ReadLine());
                switch (num)
                {
                    case 1:
                        while (true)
                        {
                            Console.WriteLine("\n请输入商品名称: ");
                            string name = Console.ReadLine();
                            names[count] = name;
                            Console.WriteLine("请输入商品价格: ");
                            double price = Convert.ToDouble(Console.ReadLine());
                            prices[count] = price;
                            count++;
                            Console.WriteLine("是否选择重新录入? y | n");
                            string flag = Console.ReadLine();
                            if (!(flag == "y"))
                            {
                                break;
                            }
                        }
                        break;
                    case 2:
                        Console.WriteLine("\n商品下标\t商品名称\t商品价格");
                        for (int i = 0; i < count; i++)
                        {
                            Console.WriteLine($"{i}\t{names[i]}\t{prices[i]}");
                        }
                        break;
                    case 3:
                        Console.WriteLine("\n商品下标\t商品名称\t商品价格");
                        for (int i = count - 1; i >= 0; i--)
                        {
                            Console.WriteLine($"{i}\t{names[i]}\t{prices[i]}");
                        }
                        break;
                    case 4:
                        Console.WriteLine("请选择要下架的商品序号: ");
                        int index = Convert.ToInt32(Console.ReadLine());
                        for (int i = index + 1; i < names.Length; i++)
                        {
                            names[i - 1] = names[i];
                            prices[i - 1] = prices[i];
                        }
                        names[count - 1] = null;
                        prices[count - 1] = 0.0;
                        count--;
                        Console.WriteLine("下架商品成功");
                        break;
                    case 5:
                        Console.WriteLine("感谢使用，再见!");
                        started = false;
                        break;
                    default:
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("\n输入的操作编号不存在！");
                        Console.ResetColor();
                        break;
                }

            }
            while (started);
        }
    }
}
